package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;
public class DVDRecords
{
    private ArrayList<DVD> dvdRecords;
    private static DVDRecords instance = null;

    private DVDRecords()
    {
        // initialise instance variables              
        dvdRecords = new ArrayList<DVD>();
    }

    public static DVDRecords getInstance()
    {
        if(instance == null)
        {
            instance = new DVDRecords();
        }

        return instance;
    }
    
    public ArrayList<DVD> getDVDList()
    {
        return  dvdRecords;
    }
        
    public void printAllDVD()
    {
        if (dvdRecords.size() !=0)
        {
            for(DVD dvd : dvdRecords)
            {
                dvd.printDVDInfo();
            }
        }
    }

    public DVD getDVD(int id)
    {
        DVD theDvd = null;
        boolean found = false;
        Iterator<DVD> it = dvdRecords.iterator();
        while(it.hasNext() && !found)
        {
            DVD dvd = it.next();
            if(dvd.getId() == id)
            {
                theDvd = dvd;
                found = true;
            }
        }        
        return theDvd;
    }

    public void removeDVD(int id)
    {        
        DVD dvd = getDVD(id);
        if(dvd != null)
        {
            dvdRecords.remove(dvd);

        }
    }

    public void addDVD(DVD dvd)
    {   
        dvdRecords.add(dvd);
    }

    public void createDVD(int id, String title, String artist, long publicationDate)
    {
        DVD dvd = new DVD(id,title,artist,publicationDate);
        addDVD(dvd);
    }

    public void updateDVD(int choise, int id, String value)
    {
        DVD dvd = getDVD(id);
        if (dvd != null) {
            if(choise == 1)
            {
                dvd.setTitle(value);
            }
            if(choise == 2)
            {
                dvd.setArtist(value);
            }
            if(choise == 3)
            {
                dvd.setPublicationDate(Long.parseLong(value));
            }

        }
    }

}
